<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts' // echarts theme
import drawMixin from "../../utils/drawMixin";

export default {
  mixins: [drawMixin],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler (val) {
        this.setOptions(val)
      }
    }
  },
  data () {
    return {
      chart: null
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    getLevelOption() {
      return [
        {
          itemStyle: {
            borderWidth: 0,
            gapWidth: 5
          }
        },
        {
          itemStyle: {
            gapWidth: 1
          }
        },
        {
          colorSaturation: [0.35, 0.5],
          itemStyle: {
            gapWidth: 1,
            borderColorSaturation: 0.6
          }
        }
      ];
    },
    setOptions ({ dataValue } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} \n {b} : {c}'
        },
        // legend: {
        //   left: 'center',
        //   bottom: '10'
        // },
        series: [
          {
            name: '各类数据数量',
            type: 'treemap',
            visibleMin: 300,
            label: {
              show: true,
              formatter: '{b}\n\n{c}'
            },
            itemStyle: {
              borderColor: '#fff'
            },
            levels: this.getLevelOption(),
            data: dataValue
          }
        ]
      })
    }
  }
}
</script>
